package vn.training.view.bean;

import javax.faces.application.ViewHandler;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

public class LocaleBean {
    
    private String language = "en";

    public void setLanguage(String language) {
        this.language = language;
    }

    public String getLanguage() {
        return language;
    }  
    
    public void handleLanguageSwitch(ValueChangeEvent valueChangeEvent) {
        refreshPage(String.valueOf(valueChangeEvent.getNewValue()));
    }
    
    public void refreshPage(String newLanguage) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        String refreshpage = facesContext.getViewRoot().getViewId();
        
        ViewHandler viewHandler = facesContext.getApplication().getViewHandler();
        
        this.language = newLanguage;
        
        UIViewRoot uiv = viewHandler.createView(facesContext, refreshpage);
        uiv.setViewId(refreshpage);

        facesContext.setViewRoot(uiv);
    }
}
